import { createRouter, createWebHistory, RouteRecordRaw } from 'vue-router'
// 引入封装的nprogress.ts
import { start, close } from '@/utils/nprogress';

const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        name: 'index',
        component: () => import('@/views/index/index.vue'),
        meta: {
            title: '博客鸭'
        }
    },
    {
        path: '/blog',
        name: 'blog',
        component: () => import('@/views/blog/index.vue'),
        meta: {
            title: '博客 - 博客鸭'
        }
    },
    {
        path: '/faq',
        name: 'faq',
        component: () => import('@/views/faq/index.vue'),
        meta: {
            title: '问答 - 博客鸭'
        }
    },
    {
        path: '/download',
        name: 'download',
        component: () => import('@/views/download/index.vue'),
        meta: {
            title: '资源下载 - 博客鸭'
        }
    },
    {
        path: '/user-blog/:id',
        name: 'user-blog',
        component: () => import('@/views/user-blog/index.vue'),
        meta: {
        }
    },
    {
        path: '/user-center/:id',
        name: 'user-center',
        component: () => import('@/views/user-center/index.vue'),
        meta: {
        }
    },
    {
        path: '/article-detail/:id',
        name: 'article-detail',
        component: () => import('@/views/article-detail/index.vue'),
        meta: {
        }
    },
    {
        path: '/faq-detail/:id',
        name: 'faq-detail',
        component: () => import('@/views/faq-detail/index.vue'),
        meta: {
        }
    },
    {
        path: '/download-detail/:id',
        name: 'download-detail',
        component: () => import('@/views/download-detail/index.vue'),
        meta: {
        }
    },
    {
        path: '/upload-question',
        name: 'upload-question',
        component: () => import('@/views/upload-question/index.vue'),
        meta: {
            title: '上传资源 - 博客鸭'
        }
    },
    {
        path: '/upload-source',
        name: 'upload-source',
        component: () => import('@/views/upload-source/index.vue'),
        meta: {
            title: '上传问题 - 博客鸭'
        }
    },
    {
        path: '/publish-blog',
        name: 'publish-blog',
        component: () => import('@/views/publish-blog/index.vue'),
        meta: {
            title: '发布博客 - 博客鸭'
        }
    },
    {
        path: '/login',
        name: 'login',
        component: () => import('@/views/login/index.vue'),
        meta: {
            title: '登录 - 博客鸭'
        }
    },
    {
        path: '/404',
        component: () => import('@/views/error-page/404.vue'),
        name: 'Page404',
        meta: { title: '404' }
    },
    { path: '/:pathMatch(.*)', redirect: '/404' }
]

const router = createRouter({
    history: createWebHistory(),
    routes,
    scrollBehavior(to, from, savedPosition) {
        // if (savedPosition) {
        //     return savedPosition
        // } else {
        //     return { top: 0 }
        // }
        return { top: 0 }
    }
})
router.beforeEach((to, from, next) => {
    start(); //---------------------------------------------->此处开始
    /* 路由发生变化修改页面title */
    if (typeof (to.meta.title) === 'string') {
        document.title = to.meta.title
    }
    const token = localStorage.getItem('token')
    const blackList = ['/user-center']
    if (!token) {
        console.log(to.path.split('/'));
        const path = to.path.split('/')[1]
        let pathStr = '/' + path
        // console.log(pathStr);
        if (blackList.indexOf(pathStr) !== -1) {
            if (to.path === '/login') {
                next()
            }
            next(`/login`)
            return
        }
    }
    next()
})
// 路由加载结束后执行
router.afterEach(() => {

    close();//---------------------------------------------->此处结束
});
export default router